package com.bbzn.device.client.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.utils.BaseApiService;
import com.bbzn.device.client.utils.BaseResponse;
import com.bbzn.device.client.utils.JsonUtils;
import com.ctg.ag.sdk.biz.AepDeviceStatusClient;
import com.ctg.ag.sdk.biz.aep_device_status.GetDeviceStatusHisInPageRequest;
import com.ctg.ag.sdk.biz.aep_device_status.GetDeviceStatusHisInPageResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wulongbo
 * @Date 2021/5/10 11:39
 * @Version 1.0
 */

@RestController
@RequestMapping("/deviceHis")
//@CrossOrigin
@Api(tags = "设备历史数据查询")
public class DeviceHisController extends BaseApiService<JSONObject> {

    public static AepDeviceStatusClient client;

    public static AepDeviceStatusClient getClient() {
        if (client == null) {
            synchronized (AepDeviceStatusClient.class) {
                if (client == null) {
                    client = AepDeviceStatusClient.newClient()
                            .appKey(YG.APP_KEY)
                            .appSecret(YG.APP_SECRET)
                            .build();
                }
            }
        }
        return client;
    }

    /***
     * 设备状态历史数据分页查询
     * @param deviceId
     * @param productId
     * @param begin_timestamp
     * @param end_timestamp
     * @param page_size
     * @param page_timestamp
     * @return
     * @throws Exception
     */
    @GetMapping("/getDeviceStatusHisInPage ")
    @OperationAnnotation(content = "设备状态历史数据分页查询")
    public BaseResponse<JSONObject> getDeviceStatusHisInPage (String deviceId, Integer productId, String begin_timestamp, String end_timestamp, Integer page_size, String page_timestamp) throws Exception {

        // 1.非空判断
        if(StringUtils.isEmpty(deviceId)){
            throw new DefinitException(406,"设备id不能为空！");
        }
        if(productId==null){
            throw new DefinitException(407,"产品id不能为空！");
        }
        if(StringUtils.isEmpty(begin_timestamp)){
            throw new DefinitException(408,"开始时间不能为空！");
        }
        if(StringUtils.isEmpty(end_timestamp)){
            throw new DefinitException(409,"结束时间不能为空！");
        }
        if(page_size==null){
            throw new DefinitException(410,"每页记录数不能为空！");
        }

        // 2.构建map
        Map<String,Object> hisMap=new HashMap<String, Object>();
        hisMap.put("productId",productId);
        hisMap.put("deviceId",deviceId);
        hisMap.put("begin_timestamp",begin_timestamp);
        hisMap.put("end_timestamp",end_timestamp);
        hisMap.put("page_size",page_size);
        hisMap.put("page_timestamp",page_timestamp);

        // 3.Map对象转换成byte数组
        byte[] body = JsonUtils.changeMapToByte(hisMap);

        // 1.创建client
        AepDeviceStatusClient client=getClient();
        GetDeviceStatusHisInPageRequest request = new GetDeviceStatusHisInPageRequest();
         request.setBody(body);
        GetDeviceStatusHisInPageResponse result=client.getDeviceStatusHisInPage(request);
        int code=result.getStatusCode();
        if(code!=200){
            throw new DefinitException(code,result.getMessage());
        }
        byte[] resBody= result.getBody();
        JSONObject jsonObject= JsonUtils.byteToJson(resBody);
        Integer resCode=jsonObject.getInteger("code");
        if(resCode!=0){
            String msg=jsonObject.getString("desc");
            throw new DefinitException(resCode,msg);
        }
        String response=jsonObject.getString("deviceStatusList");
        JSONArray array = JSONArray.parseArray(response);
        JSONObject json=new JSONObject();
        json.put("deviceStatusList",array);
        return setResultSuccess(json);
    }
}
